﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DanhMucCtr
/// </summary>
public class DanhMucCtr
{
    SqlDataAdapter da;
    DataSet ds;
    DataProvider con = new DataProvider();
	public DanhMucCtr()
	{
		
	}
    public DataTable layDanhMuc()
    {
        try
        {
            string sql = "select * from DanhMuc where TrangThai=1";
            return DataProvider.GetData(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    public DataTable layDanhMuc1()
    {
        try
        {
            string sql = "select * from DanhMuc where TrangThai=1";
            return DataProvider.GetData(sql);
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    public EDanhMuc layDanhMuc(string MaDanhMuc)
    {
        try
        {
            DataTable tableDanhMuc = DataProvider.GetData("Select * from DanhMuc Where MaDanhMuc=@MaDanhMuc and TrangThai='True'", "@MaDanhMuc", MaDanhMuc);
            EDanhMuc DanhMuc = new EDanhMuc();
            DanhMuc.MaDanhMuc = Int32.Parse(tableDanhMuc.Rows[0]["MaDanhMuc"].ToString());
            DanhMuc.TenDanhMuc = tableDanhMuc.Rows[0]["TenDanhMuc"].ToString();
            return DanhMuc;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool InsertDanhMuc(EDanhMuc DanhMuc)
    {
        try
        {
            DataProvider.ExecuteNonQuery("INSERT INTO DanhMuc VAlUES(@TenDanhMuc,@TrangThai)",
                 "@TenDanhMuc", DanhMuc.TenDanhMuc,"@TrangThai",true);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool DeleteDanhMuc(string MaDanhMuc)
    {
        try
        {
            DataProvider.ExecuteNonQuery("DELETE FROM DanhMuc WHERE MaDanhMuc=@MaDanhMuc", "@MaDanhMuc", MaDanhMuc);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
   
    public bool UpdateDanhMuc(int MaDanhMuc, string TenDanhMuc)
    {
        try
        {
            DataProvider.ExecuteNonQuery("Update DanhMuc set TenDanhMuc=@TenDM WHERE MaDanhMuc=@MaDanhMuc", "@MaDanhMuc", MaDanhMuc, "@TenDM", TenDanhMuc);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool DeleteDanhMuc1(string MaDanhMuc)
    {
        try
        {
            DataProvider.ExecuteNonQuery("Update DanhMuc set TrangThai=0 WHERE MaDanhMuc=@MaDanhMuc", "@MaDanhMuc", MaDanhMuc);
            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public DataTable GetMaDanhMuc()
    {
        try
        {
            return DataProvider.GetData("Select MAx(MaDanhMuc) as MaDM from DanhMuc");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public int demDanhMuc()
    {
        String sql = "Select count(*) from DanhMuc where TrangThai=1";
        da = new SqlDataAdapter(sql, DataProvider.GetConnection());
        ds = new DataSet();
        da.Fill(ds);
        return int.Parse(ds.Tables[0].Rows[0][0].ToString());

    }
    public EDanhMuc layDanhMuc1(string LoaiSP)
    {
        try
        {
            DataTable tablePhanLoai = DataProvider.GetData("Select * from DanhMuc Where TrangThai=1 and MaDanhMuc=@LoaiSP", "@LoaiSP", LoaiSP);
            EDanhMuc PhanLoai = new EDanhMuc();
            PhanLoai.MaDanhMuc = Int32.Parse(tablePhanLoai.Rows[0]["MaDanhMuc"].ToString());
            PhanLoai.TenDanhMuc = tablePhanLoai.Rows[0]["TenDanhMuc"].ToString();
            PhanLoai.TrangThai = tablePhanLoai.Rows[0]["TrangThai"].ToString();
            return PhanLoai;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}